package com.wy.allthingdone.activity;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wy.allthingdone.R;
import com.wy.allthingdone.menus.BrightMenu;
import com.wy.allthingdone.menus.CenterAreaTouchListsner;
import com.wy.allthingdone.menus.ProgressMenu;
import com.wy.allthingdone.menus.ReaderLayerEvenProcessor;
import com.wy.allthingdone.menus.ReaderViewMenu;
import com.wy.allthingdone.menus.StyleMenu;
import com.wy.allthingdone.menus.TextMenu;
import com.wy.hwreader.action.Action;
import com.wy.hwreader.action.ReaderProgressSaveAction;
import com.wy.hwreader.action.TxtPageTypesettingChangeToVerticalAction;
import com.wy.hwreader.action.TxtPageTypesettingChangeTohorizontalAction;
import com.wy.hwreader.action.TxtProgressAction;
import com.wy.hwreader.action.TxtStyleChangeAction;
import com.wy.hwreader.action.TxtTextSizeAddAction;
import com.wy.hwreader.action.TxtTextSizeReduceAction;
import com.wy.hwreader.action.TxtTextSortChageAction;
import com.wy.hwreader.action.ViewSettingSaveAction;
import com.wy.hwreader.beans.ReaderStyle;
import com.wy.hwreader.readermain.Book;
import com.wy.hwreader.readermain.BookProcessor;
import com.wy.hwreader.readermain.CallBack;
import com.wy.hwreader.readermain.ReaderException;
import com.wy.hwreader.readermain.ReaderException.Type;
import com.wy.hwreader.readermain.ReaderView;
import com.wy.hwreader.txtreader.ITxtPageChangeListener;
import com.wy.hwreader.txtreader.TxtFileInitProcessor;
import com.wy.hwreader.txtreader.TxtReaderContex;

/**
 * @author 黄威 2016年11月1日上午10:18:29 主页：http://blog.csdn.net/u014614038
 */
public class HwReaderPlayActivity extends Activity {
    // view
    public ReaderView readerview;
    private RelativeLayout mLoadingView;
    private RelativeLayout mNodataView;
    private RelativeLayout mTitlebar;
    private LinearLayout mToucherLayer;
    private TextView mTitle, mNodataText, mLoadingMsg;
    private ReaderViewMenu mReaderViewMenu;
    private TextMenu mTextMenu;
    private StyleMenu mStyleMenu;
    private BrightMenu mBrightMenu;
    private ProgressMenu mProgressMenu;
    // data
    final TxtReaderContex readerContex = new TxtReaderContex();
    private Handler mHander;
    private String mBookPath;
    private String mBookName;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_hw_reader_play);
        getMyIntent();
        findview();
        initdata();
        registListener();
        loadBook();

    }

    private void getMyIntent() {
        Intent intent = getIntent();
        mBookName = intent.getStringExtra("bookname");
        mBookPath = intent.getStringExtra("bookpath");
    }

    private void registListener() {

        final ReaderLayerEvenProcessor layerEvenProcessor = new ReaderLayerEvenProcessor(readerContex,
                new CenterAreaTouchListsner() {

                    @Override
                    public void onOutSideTouch() {
                        hideReaderMenu();
                    }

                    @Override
                    public void onCenterTouch() {
                        showMenu();
                    }
                });

        mToucherLayer.setOnTouchListener(new OnTouchListener() {

            @SuppressLint("ClickableViewAccessibility")
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return layerEvenProcessor.Process(event);
            }
        });

        mNodataView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                loadBook();
            }
        });

        readerContex.setOnTxtPageChangeListener(new ITxtPageChangeListener() {

            @Override
            public void onPageChange(int currentpageindex, int pageunms) {
                mProgressMenu.setPageIndex(currentpageindex, pageunms);
            }
        });

    }

    private void loadBook() {
        ShowLoadingView();
        new Thread(new Runnable() {
            public void run() {

                Book book = new Book();
                // book.BookPath = Environment.getExternalStorageDirectory() +
                // "/test5.txt";
                book.BookPath = mBookPath;
                book.BookName = mBookName;
                readerContex.mBook = book;
                BookProcessor<Type> processor = new TxtFileInitProcessor();

                processor.Process(new CallBack<Type>() {

                    @Override
                    public void onBack(Type t) {
                        if (t == Type.sucess) {
                            readerview.postInvalidate();
                            ShowReaderView();
                            initMenu();
                        } else {
                            ShowMessageView(ReaderException.getExceptionMsg(t) + ",点击重新加载");
                        }

                    }

                }, readerContex);

            }
        }).start();
    }

    private void initdata() {
        mHander = new Handler();
        readerview.setReaderContex(readerContex);
        readerContex.setReaderView(readerview);
        readerContex.mContext = HwReaderPlayActivity.this;
        readerContex.AddAction(new TxtTextSizeAddAction(readerContex));
        readerContex.AddAction(new TxtTextSizeReduceAction(readerContex));
        readerContex.AddAction(new TxtTextSortChageAction(readerContex));
        readerContex.AddAction(new ViewSettingSaveAction(readerContex));
        readerContex.AddAction(new ReaderProgressSaveAction(readerContex));
        readerContex.AddAction(new TxtProgressAction(readerContex));
        readerContex.AddAction(new TxtStyleChangeAction(readerContex));
        readerContex.AddAction(new TxtPageTypesettingChangeTohorizontalAction(readerContex));
        readerContex.AddAction(new TxtPageTypesettingChangeToVerticalAction(readerContex));

        ImageView imageView = (ImageView) findViewById(R.id.txtview_loading_img);
        imageView.setBackgroundResource(R.drawable.hwreader_loading_annimation);
        final AnimationDrawable mAnimation = (AnimationDrawable) imageView.getBackground();
        imageView.post(new Runnable() {
            @Override
            public void run() {
                mAnimation.start();
            }
        });


    }

    private void findview() {
        readerview = (ReaderView) findViewById(R.id.hwreadView);
        mTitlebar = (RelativeLayout) findViewById(R.id.hwreader_titlebar);
        mTitle = (TextView) findViewById(R.id.hwreader_title_textview);
        mLoadingView = (RelativeLayout) findViewById(R.id.hwreader__loadingview);
        mNodataView = (RelativeLayout) findViewById(R.id.hwreader_nodataview);
        mNodataText = (TextView) findViewById(R.id.txtview_nodata_text);
        mLoadingMsg = (TextView) findViewById(R.id.txtview_loading_msg);
        mToucherLayer = (LinearLayout) findViewById(R.id.hwreader_toucher_layer);
        mTitle.setText(mBookName);

    }

    private void initMenu() {
        runOnUi(new onUiRun() {

            @Override
            public void Run() {
                mReaderViewMenu = new ReaderViewMenu(readerContex.mContext);
                mTextMenu = new TextMenu(readerContex);
                mProgressMenu = new ProgressMenu(readerContex);
                mStyleMenu = new StyleMenu(readerContex);
                mBrightMenu = new BrightMenu(readerContex.mContext);
                registMenuListsner();
                readerContex.onPageChageListener();
            }
        });

    }

    private void registMenuListsner() {
        mReaderViewMenu.setOnTxtMenuClickListener(new ReaderViewMenu.TxtMenuClockListener() {

            @Override
            public void onTextMenuClicked() {
                showTextMenu();
            }

            @Override
            public void onStytleMenuClicked() {
                showStyleMenu();
            }

            @Override
            public void onProgressMenuClicked() {
                showProgressMenu();
            }

            @Override
            public void onLightMenuClicked() {
                showBrightMenu();

            }
        });

        mTextMenu.setonTxtTextChangeListener(new TextMenu.onTxtTextChangeListener() {

            @Override
            public void onTextSortChange(String textsort) {
                readerContex.mViewSetting.TexttTypeFile = textsort;
                readerContex.doAction(Action.changetextsort);
            }

            @Override
            public void onTextSizeAdd() {
                readerContex.doAction(Action.addtextsize);
            }

            @Override
            public void onTextSizeDec() {
                readerContex.doAction(Action.reducetextsize);
            }

            @Override
            public void onTextBold(Boolean isBold) {
                readerContex.mViewSetting.MakeBoldText = isBold;
                readerContex.doAction(Action.changetextsort);
            }
        });

        mProgressMenu.setonProgressChangeListener(new ProgressMenu.onProgressChangeListener() {

            @Override
            public void onProgressChange(int progress) {
                TxtProgressAction progressAction = (TxtProgressAction) readerContex.getAction(Action.progresschange);
                progressAction.setPageProgress(progress);
                readerContex.doAction(Action.progresschange);
            }
        });

        mStyleMenu.setonTxtStyleChangeListener(new StyleMenu.onTxtStyleChangeListener() {

            @Override
            public void onStyleChange(ReaderStyle readerStyle, ReaderStyle.Style style) {
                readerContex.mViewSetting.setPageBackground(readerStyle);
                readerContex.doAction(Action.backgroundstylechanged);

                if (style == ReaderStyle.Style.ancientry2) {
                    readerContex.doAction(Action.TypesettingChangetovertical);
                }

            }
        });
    }

    public void finishactivity(View v) {
        finish();

    }
    // ----------------------------------------------------------------------------------------//

    private void ShowMessageView(final String msg) {
        runOnUi(new onUiRun() {
            @Override
            public void Run() {
                mNodataText.setText(msg);
                mNodataView.setVisibility(View.VISIBLE);
                mLoadingView.setVisibility(View.GONE);
                readerview.setVisibility(View.GONE);
                mToucherLayer.setVisibility(View.GONE);

            }
        });
    }

    private void ShowReaderView() {
        runOnUi(new onUiRun() {
            @Override
            public void Run() {
                mTitlebar.setVisibility(View.GONE);
                mNodataView.setVisibility(View.GONE);
                mLoadingView.setVisibility(View.GONE);
                readerview.setVisibility(View.VISIBLE);
                mToucherLayer.setVisibility(View.VISIBLE);
            }
        });
    }

    private void ShowLoadingView() {
        runOnUi(new onUiRun() {
            @Override
            public void Run() {
                mNodataView.setVisibility(View.GONE);
                mLoadingView.setVisibility(View.VISIBLE);
                readerview.setVisibility(View.GONE);
                mToucherLayer.setVisibility(View.GONE);

            }
        });
    }

    @SuppressWarnings("unused")
    private void setLoadingMsg(final String meesage) {
        runOnUi(new onUiRun() {

            @Override
            public void Run() {
                mLoadingMsg.setText(meesage);
            }
        });
    }

    private void showMenu() {

        mReaderViewMenu.showAtLocation(getParentView(), Gravity.BOTTOM, 0, 0);
        mTitlebar.setVisibility(View.VISIBLE);

    }

    private View getParentView() {
        return HwReaderPlayActivity.this.getWindow().getDecorView();
    }

    private void hideReaderMenu() {
        mReaderViewMenu.dismiss();
        mTitlebar.setVisibility(View.GONE);
    }

    private void showTextMenu() {
        hideReaderMenu();
        mTextMenu.showAtLocation(getParentView(), Gravity.BOTTOM, 0, 0);
    }

    private void showProgressMenu() {
        hideReaderMenu();
        mProgressMenu.showAtLocation(getParentView(), Gravity.BOTTOM, 0, 0);

    }

    private void showStyleMenu() {
        hideReaderMenu();
        mStyleMenu.showAtLocation(getParentView(), Gravity.BOTTOM, 0, 0);

    }

    private void showBrightMenu() {
        hideReaderMenu();
        mBrightMenu.showAtLocation(getParentView(), Gravity.BOTTOM, 0, 0);

    }

    // -----------------------------------------------------------------------------------------//
    private void runOnUi(final onUiRun uirun) {
        getHander().post(new Runnable() {

            @Override
            public void run() {
                uirun.Run();
            }
        });
    }

    private Handler getHander() {
        return mHander;
    }

    private interface onUiRun {
        public void Run();
    }

    @Override
    protected void onDestroy() {
        if (readerContex != null) {
            readerContex.doAction(Action.saveviewsettings);
            readerContex.doAction(Action.savereaderprogress);
            if (readerContex.mCacheCenter != null)
                readerContex.mCacheCenter.clear();
            if (readerContex.mBitmapManager != null)
                readerContex.mBitmapManager.recycle();
        }
        super.onDestroy();
    }
}
